package com.jwthhealth_pub.old.report.view;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.common.LogUtil;
import com.jwthhealth_pub.common.base.BaseActivity;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.report.model.resbody.PhysDescRes;
import com.jwthhealth_pub.old.report.view.adapter.PhysDescAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;

/**
 * 指标说明
 * Created by liuzhen on 2017/10/23.
 */

public class IndicalPhysicalActivity extends BaseActivity {
    @BindView(R.id.title)
    TitleLayout title;
    @BindView(R.id.rv_data)
    RecyclerView rvData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_indicator_physical);

        ButterKnife.bind(this);

        getData();
    }

    @Override
    protected void title() {
        title.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {
                finish();
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    public void getData() {
        String id = getIntent().getStringExtra("id");
        if (id == null) return;
        final Call<PhysDescRes> _call = ApiHelper.getPhisIndeExpl(id);
        Observable<PhysDescRes.DataBean> observable = Observable.create(new ObservableOnSubscribe<PhysDescRes.DataBean>() {
            @Override
            public void subscribe(ObservableEmitter<PhysDescRes.DataBean> e) throws Exception {
                showLoadProgresbar();
                PhysDescRes response = _call.execute().body();
                if (response != null) {
                    if (response.getCode().equals("0")) {
                        PhysDescRes.DataBean data = response.getData();
                        if (data != null) {
                            e.onNext(data);
                        } else {
                            e.onError(new Throwable(getString(R.string.common_net_error)));
                        }
                    } else {
                        e.onError(new Throwable(response.getMsg()));
                    }
                } else {
                    e.onError(new Throwable(getString(R.string.common_net_error)));
                }
            }
        });

        Observer<PhysDescRes.DataBean> observer = new Observer<PhysDescRes.DataBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(PhysDescRes.DataBean o) {
                missLoadProgressbar();
                List<PhysDescRes.DataBean.SonBeanX> son = o.getSon();
                if (son != null && son.size() > 0)
                    RenderingUi(son);
                else
                    LogUtil.toast(getString(R.string.common_net_error));
            }

            @Override
            public void onError(Throwable e) {
                missLoadProgressbar();
                LogUtil.toast(e.getMessage());
            }

            @Override
            public void onComplete() {

            }
        };

        observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(observer);
    }

    private void RenderingUi(List<PhysDescRes.DataBean.SonBeanX> data) {
        rvData.setLayoutManager(new LinearLayoutManager(this));
        rvData.setAdapter(new PhysDescAdapter(this, data));
    }
}
